﻿Imports Infragistics.Win

Public Class LinkInvestorType

#Region "Constructor"
    Public Sub New(ByVal list As List(Of String))

        ' This call is required by the designer.
        InitializeComponent()
        GetValidatedControls()
        listInvType = list
        ' Add any initialization after the InitializeComponent() call.

    End Sub

#End Region
#Region "Declare"
    Dim listInvType As List(Of String) = Nothing
#End Region

#Region "FormLoad"

    Private Sub LinkInvestorType_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        If listInvType.Count > 0 Then
            MessageBox.Show("Please link all the InvestorType with the MasterInvestorType.", "Crystal", MessageBoxButtons.OK, MessageBoxIcon.Information)
            e.Cancel = True
        End If
    End Sub

    Private Sub LinkInvestorType_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        InitCombos()
        LoadFunctionality()
    End Sub
#End Region
#Region "Method"
    Private Sub GetValidatedControls()
        ValidateControls(CType(pnlLink.Controls(0), Misc.UltraPanelClientArea).Controls, True)
    End Sub
    Private Sub LoadFunctionality()
        cmbInvType.DataSource = listInvType
        cmbMasterInvestorType.DataSource = GetMasterInvestorType()
    End Sub
    Private Sub InitCombos()
        cmbMasterInvestorType.DisplayMember = "MInvestorType"
        cmbMasterInvestorType.ValueMember = "MInvestorTypeID"
    End Sub

    Public Function GetMasterInvestorType() As DataTable
        Dim ds As DataSet = Nothing
        Dim dtMasterInvestor As New DataTable
        ds = cAddModifyInvestor.GetAllMasterInvetorType()
        dtMasterInvestor = ds.Tables(0)
        ' DropDownAtFirst(dtMasterInvestor, "MInvestorTypeID", "MInvestorType", m_strNewIDValue, m_strNewTextValue)
        Return dtMasterInvestor
    End Function

    Private Function DropDownAtFirst(ByRef dataTable As DataTable, ByVal idColumn As String, ByVal valueColumn As String, ByVal rowValue As Integer, ByVal rowText As String) As Boolean
        If dataTable Is Nothing Then Return True
        Dim drDataRow As DataRow
        drDataRow = dataTable.NewRow
        drDataRow.Item(idColumn) = rowValue
        drDataRow.Item(valueColumn) = rowText
        dataTable.Rows.InsertAt(drDataRow, 0)
        Return True
    End Function

    Private Function IsValid() As Boolean
        If Not (Me.ValidateChildren()) Then
            ValidationManager.IsButtonClick = False
            'FocusOnEmptyControl()
            Return True
        End If
        ValidationManager.IsButtonClick = False
        Return False
    End Function
#End Region

    Private Sub btnSaveInv_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSaveInv.Click
        ValidationManager.IsButtonClick = True
        If IsValid() Then
            Return
        End If
        Dim obj As New cAddModifyInvestor
        obj.InsertInvestorTypeLinking(cmbMasterInvestorType.SelectedValue, cmbInvType.Text)
        listInvType.Remove(cmbInvType.Text)
        If listInvType.Count = 0 Then
            Me.Close()
        Else
            cmbInvType.DataSource = Nothing
            cmbInvType.DataSource = listInvType
        End If
    End Sub

    Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
        Me.Close()
    End Sub
End Class